package com.njsf.service.impl;

import com.njsf.dao.CartMapper;
import com.njsf.dto.ResponseJson;
<<<<<<< HEAD
import com.njsf.excepiton.BusinessException;
=======
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import com.njsf.pojo.Cart;
import com.njsf.pojo.CartPlus;
import com.njsf.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

<<<<<<< HEAD
import java.io.FileInputStream;
=======
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Override
<<<<<<< HEAD
    public ResponseJson addCart(Cart cart) throws Exception{
        ResponseJson responseJson = null;
        try {
            responseJson = null;
            Cart c = cartMapper.findByUserAndBook(cart.getUserId(), cart.getBookId());
            if (c != null){
                throw new BusinessException("已经加入购物车!");
                //responseJson = new ResponseJson(101,"已加入购物车!");
            }else{
                int flag = cartMapper.add(cart);
                int i = 1/0;
                if (flag == 1){
                    responseJson = new ResponseJson(100,"加入成功!");
                }else{
                    throw new BusinessException("加入失败!");
                    //responseJson = new ResponseJson(101,"已加失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("服务器异常，请联系管理员!");
=======
    public ResponseJson addCart(Cart cart) {
        ResponseJson responseJson = null;
        Cart c = cartMapper.findByUserAndBook(cart.getUserId(), cart.getBookId());
        if (c != null){
            responseJson = new ResponseJson(101,"已加入购物车!");
        }else{
            int flag = cartMapper.add(cart);
            if (flag == 1){
                responseJson = new ResponseJson(100,"加入成功!");
            }else{
                responseJson = new ResponseJson(101,"已加失败!");
            }
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
        }
        return responseJson;
    }

    @Override
    public List<CartPlus> findAllByUser(Integer userId) {
        return cartMapper.findAllByUser(userId);
    }


}
